package cn.interestingshop.service.goods;

import cn.interestingshop.dao.GoodsDao;
import cn.interestingshop.dao.GoodsDaoImpl;
import cn.interestingshop.entity.Goods;
import cn.interestingshop.utils.DataSourceUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodsServiceImpl implements GoodsService{
    @Override
    public Goods selectOne(Integer id) {
        Connection conn=null;
        Goods goods=new Goods();

        try {
            conn= DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);

            goods=goodsDao.selectOne(id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return goods;
    }

    @Override
    public List<Goods> selectAll(String keyWord) {
        Connection conn=null;
        List<Goods> goods=new ArrayList<>();

        try {
            conn= DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);

            goods=goodsDao.selectAll(keyWord);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }



        return goods;
    }

    @Override
    public List<Goods> handlePageList(List<Goods> list,Integer currentPage,Integer pageSize) {
        List<Goods> currentPageList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
            for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) {
                Goods wordFrequencyDTO = list.get(currIdx + i);
                currentPageList.add(wordFrequencyDTO);
            }

        }
        return currentPageList;
    }

    @Override
    public List<Goods> selectAll(Integer currentPageNo, Integer pageSize) {
        Connection conn=null;

        List<Goods> list=new ArrayList<>();

        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);

            list=goodsDao.selectAll(currentPageNo,pageSize);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return list;
    }

    @Override
    public int findUserCount() {
        Connection conn=null;

        int rs=0;

        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);

            rs=goodsDao.findUserCount();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return rs;
    }


    @Override
    public int findUserCount(List<Goods> goods) {
        Connection conn=null;

        int rs=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);
            rs=goodsDao.findUserCount(goods);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return rs;
    }

    @Override
    public List<Goods> selectOne(Integer id, Integer type,Integer currentPageNo, Integer pageSize) {

        Connection conn=null;

        List<Goods> goods=new ArrayList<>();
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);
            conn=DataSourceUtil.openConnection();

            goods=goodsDao.selectOne(id,type,currentPageNo,pageSize);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return goods;
    }

    @Override
    public int findUserCount(Integer id, Integer type) {
        Connection conn=null;

        int count=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);


            count=goodsDao.findUserCount(id,type);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return count;
    }

    @Override
    public int kjKc(int id, int stock) {


        Connection conn=null;

        int total=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);


            total=goodsDao.kjKc(id,stock);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return total;
    }

    @Override
    public int deleteById(int id) {
        Connection conn=null;

        int total=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);


            total=goodsDao.deleteById(id);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return total;
    }

    @Override
    public int executeUpdate(Goods goods) {
        Connection conn=null;

        int total=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);


            total=goodsDao.executeUpdate(goods);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return total;
    }

    @Override
    public int insert(Goods goods) {
        Connection conn=null;

        int total=0;
        try {
            conn=DataSourceUtil.openConnection();
            GoodsDao goodsDao=new GoodsDaoImpl(conn);


            total=goodsDao.insert(goods);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DataSourceUtil.closeConnection(conn);
        }


        return total;
    }


}
